<template>
    <div class="person">
        <h1>情况四：监视【ref】或【reactive】定义的【对象类型】数据中的某个属性</h1>
        <h2>姓名：{{ person.name }}</h2>
        <h2>年龄：{{ person.age }}</h2>
        <h2>汽车：{{ person.car.c1 }}、{{ person.car.c2 }}</h2>
        <button @click="changeName">修改名字</button>
        <button @click="changeAge">修改年龄</button>
        <button @click="changeC1">修改第一台车</button>
        <button @click="changeC2">修改第二台车</button>
        <button @click="changeCar">修改整个车</button>
    </div>
</template>

<script lang="ts" setup name="Person">
import { reactive, watch } from 'vue';

let person = reactive({
    name: '张三',
    age: 98,
    car: {
        c1: '奔驰',
        c2: '宝马'
    }
})

const changeName = () => {
    person.name += '~'
}
const changeAge = () => {
    person.age += 1
}
const changePerson = () => {
    Object.assign(person, { name: '吊毛', age: 97 })
}
const changeC1 = () => {
    person.car.c1 = '迈巴赫'
}
const changeC2 = () => {
    person.car.c2 = '保时捷'
}
const changeCar = () => {
    Object.assign(person.car, { c1: '路虎', c2: '兰博基尼' })
}
// 监视，情况四：监视响应式对象中的某个属性，且该属性是基本类型的，要写成函数式
/* watch(()=> person.name,(newValue,oldValue)=>{
  console.log('person.name变化了',newValue,oldValue)
}) */

// 监视，情况四：监视响应式对象中的某个属性，且该属性是对象类型的，可以直接写，也能写函数，更推荐写函数
watch(() => person.car, (newValue, oldValue) => {
    console.log('person.car 变化了', newValue, oldValue)
}, { deep: true })

</script>

<style scoped>
.person {
    background-color: skyblue;
    box-shadow: 0 0 10px;
    border-radius: 10px;
    padding: 20px;
}

button {
    margin: 0 5px;
}

li {
    font-size: 20px;
}
</style>